package sg.nus.iss.vmcs.guilisteners;

import java.awt.TextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import sg.nus.iss.vmcs.controllers.StoreController;
import sg.nus.iss.vmcs.gui.MessageDialog;
import sg.nus.iss.vmcs.utils.StoreType;

/**
 * Action Listener for the Store Viewer used in the Machinery Panel <br><br>
 * Modifications:
 * <li>Philip - 28/12/2010 - Removed temporary variables in actionPerformed</li>
 * <br><br>
 * @author Pang Pingli
 * @author Philip Chew
 */
public class StoreViewerListener implements ActionListener {
	
	/** Type of Store */
	private StoreType        type;
	
	/** Index of Item */
	private int              item;
	
	/** Store Controller */
	private StoreController  storeCtrl;

	/**
	 * Creates an instance of the Store Viewer Listener
	 * @param type Indicator for the Type of Store
	 * @param item Index of the item
	 * @param sctrl Reference to StoreController
	 */
	public StoreViewerListener(StoreType type, int item, StoreController sctrl) {
	    this.type = type;
	    this.item = item;
	    storeCtrl = sctrl;
	}
	
	/**
	 * Implementation of actionPerformed
	 */
	public void actionPerformed(ActionEvent e) {
	    try {
	    	int qty = Integer.parseInt(((TextField) e.getSource()).getText());
	    	storeCtrl.changeStoreQuantity(type, item, qty);
	    } 
	    catch (NumberFormatException ee) {
	    	new MessageDialog(storeCtrl.getMainController().getMachineryController().getMachinerySimulatorPanel(), 
	    			MessageDialog.NUMBER_FORMAT_ERROR);
	    	return;
	    }
	}
}